/*
 * GuildFraktionInfo - list the reputation and the guild level of all members of a wow guild
 * Copyright (C) [2012 - 2013] [www.marantis.de]
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public 
 * License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later 
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not, 
 * see <http://www.gnu.org/licenses/>.
 */
package de.marantis.app.wow.gfi.helper;

import de.marantis.app.wow.gfi.beans.ReputationBean;
import java.awt.Color;
import org.apache.log4j.Logger;

/**
 * In this class we analyze the [armoryContent] reputation list for on member<br>
 * <br> 
 * Expl: http://eu.battle.net/wow/de/character/[REALM]/[CHAR]/reputation/tabular
 *
 */
public class ArmoryReputationHelper {

    private final static Logger log = Logger.getLogger(ArmoryReputationHelper.class);
    
    public static ReputationBean getReputation(String armoryContent) {
        try {
            if (armoryContent == null || armoryContent.length() <= 0) {
                return null;
            }

            //*** jump to the reputation table ***
            String search = "tbody";
            int pos = armoryContent.indexOf(search);
            if (pos < 0) {
                return null;
            }

            //*** jump to the guild ***
            search = "/wow/de/faction/guild";
            pos = armoryContent.indexOf(search, pos);
            if (pos < 0) {
                return null;
            }

            //*** search for the Rank ***
            search = "rank-";
            pos = armoryContent.indexOf(search, pos);
            if (pos < 0) {
                return null;
            }

            //*** search for the closing " ***
            String search2 = "\"";
            int pos2 = armoryContent.indexOf(search2, pos);
            if (pos2 < 0) {
                return null;
            }

            //*** fraction level as number ***
            String strRankLevel = armoryContent.substring(pos + search.length(), pos2);
            strRankLevel = strRankLevel.trim();
            int rankIntLevel = CommonHelper.String2Int(strRankLevel);
            if (rankIntLevel < 0) {
                return null;
            }

            //*** search for the reputation detail information ***
            search = "faction-score\">";
            pos = armoryContent.indexOf(search, pos2);
            if (pos < 0) {
                return null;
            }

            //*** search for the closing / ***
            search2 = "/";
            pos2 = armoryContent.indexOf(search2, pos);
            if (pos2 < 0) {
                return null;
            }

            //*** reputation detail as number ***
            String strRankScore = armoryContent.substring(pos + search.length(), pos2);
            strRankScore = strRankScore.trim();
            int rankScore = CommonHelper.String2Int(strRankScore);
            if (rankScore < 0) {
                return null;
            }

            //*** return reputation result ***
            ReputationEnum rep = ReputationEnum.getNewWithInt(rankIntLevel);
            ReputationBean repBean = new ReputationBean();
            repBean.setScore(rankScore);
            repBean.setReputation(rep);
            return repBean;

        } catch (Exception e) {
            log.error(e);
            return null;
        }
    }

    /*
     * TODO: should be replaced with a locale property 
     */
    public static String getReputationText_DE(ReputationEnum reputation) {
        try {
            switch (reputation) {
                case undefined:
                    return "hasserfüllt";
                case hated:
                    return "hasserfüllt";
                case hostile:
                    return "feindseilig";
                case unfriendly:
                    return "unfreundlich";
                case neutral:
                    return "neutral";
                case friendly:
                    return "freundlich";
                case honoured:
                    return "wohlwollend";
                case revered:
                    return "respektvoll";
                case exalted:
                    return "ehrfürchtig";
                default:
                    return "";
            }

        } catch (Exception e) {
            log.error(e);
            return null;
        }
    }

    public static Color getReputationColor(ReputationEnum reputation) {
        try {
            switch (reputation) {
                case undefined:
                    return Color.WHITE;
                case hated:
                    return Color.RED;
                case hostile:
                    return Color.ORANGE;
                case unfriendly:
                    return Color.YELLOW;
                case neutral:
                    return Color.LIGHT_GRAY;
                case friendly:
                    return new Color(128, 255, 128);
                case honoured:
                    return new Color(128, 255, 0);
                case revered:
                    return new Color(0, 255, 0);
                case exalted:
                    return new Color(0, 128, 0);
                default:
                    return new Color(0, 128, 128);
            }

        } catch (Exception e) {
            log.error(e);
            return null;
        }
    }

    public static String getReputationScoreLevelText(int score, ReputationEnum rep) {
        try {
            if (rep == null) {
                return null;
            }

            StringBuffer sb = new StringBuffer("");
            sb.append(score);
            sb.append(" / ");

            switch (rep) {
                case undefined:
                    sb = new StringBuffer("");
                    break;
                case hated:
                    sb.append("36000");
                    break;
                case hostile:
                    sb.append("3000");
                    break;
                case unfriendly:
                    sb.append("3000");
                    break;
                case neutral:
                    sb.append("3000");
                    break;
                case friendly:
                    sb.append("6000");
                    break;
                case honoured:
                    sb.append("12000");
                    break;
                case revered:
                    sb.append("21000");
                    break;
                case exalted:
                    sb.append("999");
                    break;
                default:
                    sb.append("");
            }

            return sb.toString();

        } catch (Exception e) {
            log.error(e);
            return null;
        }
    }
}
